package com.mycompany.inventory.persistence;

import com.mycompany.common.AbstractDao;
import com.mycompany.common.jdbc.ConnectionDatabase;
import com.mycompany.inventory.pojo.PaymentTerm;
import com.mycompany.inventory.pojo.PriceCurrency;

import java.util.ArrayList;
import java.util.List;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by My Company.
 * User: loitran
 * Date: Feb 29, 2012
 * Time: 1:53:19 PM
 * com.mycompany.inventory.persistence.
 */
public class PriceCurrencyDAO extends AbstractDao<PriceCurrency>{
	
	private Connection con = this.connect;
	public static final String SELECT_ALL_SQL = "select * from oce_price";
	public static final String SELECT_SQL = "select * from oce_price where priceId = ?";
	
	public PriceCurrency create(PriceCurrency obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public PriceCurrency update(PriceCurrency obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
		return obj;
	}
	
	public PriceCurrency find(long id){
		List<PriceCurrency> prices = new ArrayList<PriceCurrency>();
		try{
			PreparedStatement ps = con.prepareStatement(SELECT_SQL, PreparedStatement.RETURN_GENERATED_KEYS);
			ps.setLong(1,id);
			prices = convert(ConnectionDatabase.executeQuery(ps));
		}catch (Exception e) {
			// TODO: handle exception			
		}
		return prices.size() == 0 ? null : prices.get(0);
	}
	
	public List<PriceCurrency> findAll(){
		List<PriceCurrency> prices = new ArrayList<PriceCurrency>();
		try{
			prices = convert(ConnectionDatabase.executeQuery(con.prepareStatement(SELECT_ALL_SQL, PreparedStatement.RETURN_GENERATED_KEYS)));
		}catch (Exception e) {
			// TODO: handle exception
			prices = null;
		}
		return prices;
	}
	
	public void delete(PriceCurrency obj){
		try{
			
		}catch (Exception e) {
			// TODO: handle exception
		}
	}
	
	private List<PriceCurrency> convert(ResultSet rs) throws SQLException {
		List<PriceCurrency> prices = new ArrayList<PriceCurrency>();
		while (rs.next()) {
	        // Get the data from the row using the column index
	        String id = rs.getString(1);
	        String name = rs.getString(2);
	        int currencyId = rs.getInt(3);
	        prices.add(new PriceCurrency(id, name, currencyId));
	    }
		rs.close();
		return prices;
	}
}
